﻿using System;

using System.Collections.Generic;
using System.Text;

using Silvermoon.Shapes;
using System.Drawing;

using Silvermoon.OpenGL;
using Silvermoon.Images;
using Silvermoon.Controls.Base;

using Silvermoon.Windows;
using Silvermoon.UI;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Checkbox control.
    /// </summary>
    public class CheckBox : ToggleButtonBase
    {
        #region statics

        public static PropertyAccessor<CheckBox, float> OffsetProperty = PropertyAccessor.Register<CheckBox, float>("Offset", (e) => (e).Offset, (e, v) => (e).Offset = v);

        static CheckBox()
        {

            VisualStateManager.RegisterClassManager(typeof(CheckBox), CheckboxStateManagers.CheckboxManager);

        }
        #endregion
        #region accessors

        public static readonly PropertyAccessor<CheckBox, float> YRotationProperty = PropertyAccessor.Register<CheckBox, float>(
            "YRotation",
            (c) => c.rotation.Angle,
            (c, v) => { c.rotation.Angle = v; c.Invalidate(InvalidationFlag.Transformation); });

        #endregion
        #region fields

        private RotateTransform rotation;
        private float offset;
        private Shape checkShape;
        private Shape content;

        #endregion
        #region ctors

        public CheckBox()
            : base()
        {
            rotation = new RotateTransform { Axis = RotateAxis.YAxis, OrigX = 1f };
            Transformation += rotation;
        }

        #endregion
        #region props

        public Shape CheckShape
        {
            get
            {
                if (checkShape == null)
                {
                    checkShape = CreateDefaultCheckShape();
                    Shapes.AddLast(checkShape);
                }
                return checkShape;
            }

            set
            {
                if (checkShape != value)
                {
                    if (checkShape != null)
                    {
                        Shapes.Replace(checkShape, value);
                        checkShape.Dispose();
                    }
                    checkShape = value;
                    if (checkShape != null)
                    {
                        Shapes.AddLast(checkShape);
                    }
                }
            }
        }

        public Shape Content
        {
            get
            {
                if (content == null)
                {
                    content = CreateDefaultContent();
                    if (content != null) Shapes.AddLast(content);
                }
                return content;
            }

            set
            {
                if (content != value)
                {
                    if (content != null)
                    {
                        Shapes.Replace(content, value);
                        content.Dispose();
                    }
                    content = value;
                    if (content != null)
                    {
                        Shapes.AddLast(content);
                    }
                }
            }
        }

        protected float Offset
        {
            get { return offset; }
            set
            {
                if (offset != value)
                {
                    offset = value;
                    Invalidate(InvalidationFlag.Appearance);
                }
            }
        }

        #endregion
        #region methods

        protected override Shape CreateDefaultBackground()
        {
            return new BorderShape { BorderSize = 4, Size = Control.ScaledSize(48, 48), UseTextColor = true };
        }

        protected virtual Shape CreateDefaultCheckShape()
        {
            return new ImageShape { Image = SysImages.Check, Size = Control.ScaledSize(48, 48), UseTextColor = true, Color = Color.Empty };
        }

        protected virtual Shape CreateDefaultContent()
        {
            return null;
        }

        protected override void OnLayout()
        {
            //// base.OnLayout();
            if (CheckFlag(InvalidationFlag.Size))
            {
                int left = 0;
                Shape background = Background;
                if (background != null)
                {
                    background.Location = new Point(0, (Height - background.Height) / 2);
                    left = background.Width + 16;
                    if (chrome != null) chrome.Bounds = background.Bounds;
                }
                Shape check = CheckShape;
                if (check != null)
                {
                    int x = left == 0 ? 0 : (left - 16 - check.Width) / 2;

                    check.Location = new Point(x, (Height - check.Height) / 2);
                    if (left == 0) left = check.Width + 16;
                }
                Shape content = Content;
                if (content != null)
                {
                    content.Location = new Point(left, (Height - content.Height) / 2);
                    content.Size = content.Size;
                }
            }
            if (CheckFlag(InvalidationFlag.Appearance))
            {
                Shape check = CheckShape;
                if (check != null)
                {
                    check.Opacity = (int)(255f * Offset);
                }
            }
        }

        #endregion
    }


}
